define(['app'], function(app) {
	'use strict';

	var MIN_YEAR = 2016;
	var PAGE_LIMIT = 10;

	var controller = function controller($scope, Redpocket, $window, $timeout, $ionicSlideBoxDelegate, GoPage, $ionicPopup, Auth, GoDialog) {
		$scope.isLoad = false; //可以加载
		$scope.selectIndex = 0;

		$scope.doSelectStatus = function(type){
			//id是值 name是显示
			if(type == 1){
				GoDialog.showSelect("类型",$scope.red_data.status_items,$scope.red_data.status_select,function(data){
					$scope.red_data.status = data.id;
					$scope.red_data.status_select = data;
				});
			}else{
				GoDialog.showSelect("类型",$scope.post_data.status_items,$scope.post_data.status_select,function(data){
					$scope.post_data.status = data.id;
					$scope.post_data.status_select = data;
				});
			}
		}

		$scope.doSelectYear = function(type){
			var list = new Array();
			$scope.year_list.forEach(value => {
				var obj = {id:value,text:value};
				list.push(obj);
			});
			if(type == 1){
			//id是值 name是显示
			GoDialog.showSelect("请选择年份",list,$scope.red_data.year_select,function(data){
				$scope.red_data.year = data.id;
				$scope.red_data.year_select = data;
			});
		}else{
			GoDialog.showSelect("请选择年份",list,$scope.post_data.year_select,function(data){
				$scope.post_data.year = data.id;
				$scope.post_data.year_select = data;
			});
		}
		}
		// 年份列表下拉框
		var nowYear = (new Date()).getFullYear();
		$scope.year_list = [];
		for (var i=0; i<3 && nowYear-i >= MIN_YEAR; i++) {
			$scope.year_list.push(nowYear - i);
		}

		// 收红包数据
		$scope.red_data = {
			inited: false,

			cPage: 1, tPage: null, isend: false,
			year: nowYear+'', status: 'ready',

			confirm_count: 0, confirm_money: 0, ready_count: 0, ready_money: 0,
			packets: [
				// {
				// 	name: '大全五金大家发财',
				// 	money: 5000,
				// 	mtime: '2017/3/7 15:10',
				// 	member_name: '上海大全五金公司',
				// 	status: 'ready'
				// },
			],
			status_items = [{id:"",name:"全部"},{id:"ready",name:"未领取"},{id:"confirm",name:"已领取"},{id:"return",name:"已超时"}],
			status_select = {id:"",name:"全部"},
			year_select = {id:"",text:"请选择年份"}
		}

		// 发红包数据
		$scope.post_data = {
			inited: false,

			cPage: 1, tPage: null, isend: false,
			year: nowYear+'', status: 'ready',

			count: 0, money: 0,
			packets: [
				// {
				// 	name: '大全五金大家发财',
				// 	money: 5000,
				// 	mtime: '2017/3/7 15:10',
				// 	member_name: '上海大全五金公司',
				// 	status: 'ready'
				// },
			],
			status_items = [{id:"",name:"全部"},{id:"ready",name:"未领取"},{id:"confirm",name:"已领取"},{id:"return",name:"已超时"}],
			status_select = {id:"",name:"全部"},
			year_select = {id:"",text:"请选择年份"}
		}

		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 268 + "px"
			};
			$scope.myObj2 = {
				"height": all - 226 + "px"
			};

			$scope.init_page($scope.selectIndex || 0, true);
		}, false);

		//滑动时候触发
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
			var data = (index === 0) ? $scope.red_data : $scope.post_data;
			if (!data.inited) {
				$scope.init_page(index, true);
			}
		};
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.init_page($scope.selectIndex, true, 'refresh');
		};

		//加载更多
		$scope.doRefreshMore = function() {
			if ($scope.isLoad) return;
			$scope.isLoad = true;

			$scope.init_page($scope.selectIndex, false, 'more');
		};

		// $scope.sendredpocket = function() {
		// 	GoPage.go_states("sendredpocket");
		// }

		// // 红包详细
		// $scope.showRedpocketDetail = null;
		// $scope.openRedDetail = function(id) {
		// 	$scope.showRedpocketDetail = $scope.showRedpocketDetail ==id ? null : id;
		// }

		$scope.init_page = function (index, isInit, completeFlag) {
			var Infos = Auth.get_loginInfo();

			if (!Infos) {
				GoDialog.toast('未登录');
				GoPage.go_tabPage('home');
				return;
			}

			var data = (index === 0) ? $scope.red_data : $scope.post_data;

			if (isInit) data.isend = false;

			if (data.isend) return;

			var param = {
				year: data.year,
				received_status: data.status,
				pagelimit: PAGE_LIMIT,
				nPage: isInit ? 1 : (data.cPage + 1),

				session: Infos.user_id
			}

			var fnGetData = (index === 0) ? Redpocket.get_getlist : Redpocket.get_postlist;

			fnGetData(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				data.inited = true;

				var result = res.result;

				data.cPage = result.page.cPage;
				data.tPage = result.page.tPage;

				if (data.cPage == data.tPage) data.isend = true;

				if (index === 0) {
					data.confirm_count = result.confirm_count || 0; 
					data.confirm_money = result.confirm_money || 0; 
					data.ready_count = result.ready_count || 0; 
					data.ready_money = result.ready_money || 0; 
				} else {
					data.count = result.count || 0; 
					data.money = result.money || 0; 
				}

				if (param.nPage == 1 || isInit) {
					data.packets = result.packets;
				} else {
					data.packets = data.packets || [];
					result.packets = result.packets || [];
					for (var i=0; i<result.packets.length; i++) {
						data.packets.push(result.packets[i]);
					} 
				}
				
			}, null, function() {
				if (completeFlag === 'refresh')	$scope.$broadcast('scroll.refreshComplete');
				if (completeFlag === 'more') {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 2000);
				}
			})
		}

		$scope.filterChange = function() {
			$scope.init_page($scope.selectIndex, true);
		}
		
		var myPopup_red;
		$scope.postred_detail = {
			list: [1, 2]
		}
		$scope.showPopup_red = function(index) {//绑定数据时要注意，如果是数组，则通过$index传入下标；如果是json，则传入key
			var p = null
			var packets = $scope.post_data.packets;
			if (packets) {
				p = packets[index];
			}

			if (p && p.confirm_member && p.received_status == 'confirm') {
				
				p.confirm_member = p.confirm_member || []
				$scope.postred_detail = p;

				myPopup_red = $ionicPopup.show({//show()
					templateUrl: 'templates/modal/redbag_detail.html',
					title: "发红包",
					scope: $scope,//绑定数据，必须写这行
					buttons: [{
						text: "确定",
						type: "button-assertive", 
						onTap: function() {// 当点击保存按钮时，执行该函数
							//console.log($scope.scope.reason);
							//return $scope.scope.reason;//
						}
					}]
				});
			}


		};
		
		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup_red) {　　　　　　
					myPopup_red.close();　　　　
				}　　　　　
			}
		});
		
		$scope.recive_red = function(packet_id) {
			var Infos = Auth.get_loginInfo();

			if (!Infos) {
				GoDialog.toast('未登录');
				GoPage.go_tabPage('home');
				return;
			}

			var param = {
				packet_id: packet_id,
				session: Infos.user_id
			}

			Redpocket.receive(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}

				$scope.filterChange();
				
			}, null, function() {
				if (completeFlag === 'refresh')	$scope.$broadcast('scroll.refreshComplete');
				if (completeFlag === 'more') {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 2000);
				}
			})
		}
	}

	controller.$inject = ['$scope', 'Redpocket', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', '$ionicPopup', 'Auth', 'GoDialog'];
	
	return app.registerController("redpocketctrl",controller);
});